cc.Class({
    extends: cc.Component,

    properties: {
        targetNode: cc.Node,
        knifeNode: cc.Node,
        knifePrefab: cc.Prefab,
        scoreLabel: cc.Label,
        resetBtn: cc.Button,
        homeBtn: cc.Button,
        failAudio: {
            type: cc.AudioClip,
            default: null
        },
        successAudio: {
            type: cc.AudioClip,
            default: null
        },

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // 允许标记
        this.canThrow = true
        this.targetNode.zIndex = 1

        // 速度
        this.targetRotation = 3

        // 飞刀
        this.knifeNodeArr = []

        this.timeTaskSpeed = setInterval(() => {
            this.changeSpeed()
        }, 2000)


        this.node.on('touchstart', this.throwKnife, this)

        this.resetBtn.node.on('click', this.resetFun, this)

        this.homeBtn.node.on('click', this.toHome, this)

        this.init()
    },

    // 返回首页
    toHome() {
        cc.director.loadScene('index')
    },

    // start () {
        
    // },

    init() {
        this.score = 0
    },

    resetFun() {
        // this.knifeNodeArr = []
        // this.score = 0
        // this.updateScore(0)

        cc.director.loadScene('game')
    },

    updateScore(num) {
        this.score += num
        if(this.score <= 0) {
            this.score = 0;
        }
        this.scoreLabel.string = this.score
    },


    onDestroy() {
        this.node.off('touchstart', this.throwKnife, this);
        clearInterval(this.timeTaskSpeed)
    },

    

    update (dt) {
        this.targetNode.angle = (this.targetNode.angle + this.targetRotation) % 360

        // 旋转飞刀
        this.knifeNodeArr.forEach(element => {
            element.angle = (element.angle + this.targetRotation) % 360
            
            let red = Math.PI * (element.angle - 90) / 180
            let r = this.targetNode.width/2
            element.x = this.targetNode.x + r * Math.cos(red)
            element.y = this.targetNode.y + r * Math.sin(red)
            

        });
    },

    changeSpeed() {
        let dierction = Math.random() > 0.5 ? -1 : 1
        let speed = 1.5 + Math.random() * 2
        this.targetRotation = speed * dierction
        console.log(this.targetRotation)
    },

    // 新生成一个飞刀
    createKnife(isSuccess) {
        let knifeNode = cc.instantiate(this.knifePrefab)
        this.node.addChild(knifeNode)
        // knifeNode.setPosition(this.knifeNode.position)
        
        knifeNode.setPosition(cc.v2(0, -300))
        knifeNode.angle = 0


        if(isSuccess) {
            this.knifeNodeArr.push(this.knifeNode)
        }

        this.knifeNode = knifeNode

        this.canThrow = true
    },


    // 扔飞刀
    throwKnife() {
        if(this.canThrow) {
            this.canThrow = false
            this.knifeNode.runAction(cc.sequence(
                cc.moveTo(0.15, cc.v2(this.knifeNode.x, this.targetNode.y - this.targetNode.width/2)),
                cc.callFunc(() => {
                    let isHit = false
                    let gap = 15

                    for (let element of this.knifeNodeArr) {
                        if(Math.abs(element.angle) < gap || Math.abs(360 - element.angle) < gap) {
                            isHit = true
                            break
                        }
                    }


                    if(isHit) {
                        this.updateScore(-2)
                        //失败音效
                        cc.audioEngine.playEffect(this.failAudio, false);

                        this.knifeNode.runAction(cc.sequence(
                            cc.spawn(
                                cc.moveTo(1, cc.v2(this.knifeNode.x, -cc.winSize.height)),
                                cc.rotateTo(0.25, 30)
                            ),
                            cc.callFunc(() => {
                                
                                // 失败扣2分
                                this.createKnife(0)


                            })
                        ))
                    }else {
                        this.updateScore(1)
                        cc.audioEngine.playEffect(this.successAudio, false);
                        this.createKnife(1)

                        // 得分

                    }
                })
            ))
        }
    }

});
